package leetcode;

import java.util.Stack;

public class p155最小栈 {
    //设计一个支持 push ，pop ，top 操作，并能在常数时间内检索到最小元素的栈。
    private Stack<Integer> normalStack=new Stack<>();
    private Stack<Integer> minStack=new Stack<>();
    public p155最小栈() {

    }


    public void push(int val) {
        normalStack.push(val);

        //进入minStack时需要考虑
        //1.若minStack栈为空，则val直接进栈
        //2.若minStack.peek()>val，则val进栈
        //3.若minStack.peek()<=val,则minStack.peek()再次进栈
        if(minStack.isEmpty() || minStack.peek()>val) {
            minStack.push(val);
        }else {
            minStack.push(minStack.peek());
        }

    }

    public void pop() {
        normalStack.pop();
        minStack.pop();

    }

    public int top() {
        //弹出普通栈顶元素
        return normalStack.peek();

    }

    public int getMin() {
        return minStack.peek();
    }
}
